package com.student.servlet.admin;
//@Classname AdminAddServlet
//@time 20:27
//OMEN

import com.alibaba.fastjson.JSON;
import com.student.dao.AdminInfoDao;
import com.student.dao.impl.AdminInfoDaoImpl;
import com.student.entity.AdminInfo;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet("/admin/admin-add.let")
public class AdminAddServlet extends HttpServlet {
    private AdminInfoDao dao=new AdminInfoDaoImpl();

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.reset();
        response.setContentType("application/json;charset=UTF-8");


        //将数据库表内容设置到实体对象中
        //如果非String需要转换；
        //适当校验
        try {
        AdminInfo ad=new AdminInfo();

        ad.setAdminId(Integer.parseInt(request.getParameter("adminID")));
        ad.setAdminName(request.getParameter("adminName"));
        ad.setAdminPassword(request.getParameter("adminPassword"));


        try {
            dao.insert(ad);
            response.getWriter().print("{\"ok\":true}");
        }catch (Exception ex){
            throw new  ServletException("重复",ex);
        }
        response.flushBuffer();
     }catch (Exception ex) {
            response.getWriter().print("{\"ok\":false,\",msg\":\""+ex.getMessage()+"\"}");
        }
    }
}
